package com.atguigu.flink.edu.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.atguigu.flink.edu.common.GmallConfig;

import java.sql.Connection;
import java.sql.SQLException;

public class DruidDSUtil {
    private static DruidDataSource druidDataSource;
    static {
        System.out.println("获取德鲁伊连接池");
        //获取连接池
        druidDataSource = new DruidDataSource();
        //设置驱动全类名
        druidDataSource.setDriverClassName(GmallConfig.PHOENIX_DRIVER);
        //设置连接URL
        druidDataSource.setUrl(GmallConfig.PHOENIX_URL);
        //初始化连接池中的数量
        druidDataSource.setInitialSize(5);
        //设置同时活跃的最大连接数量
        druidDataSource.setMaxActive(20);
        druidDataSource.setMinIdle(3);
        druidDataSource.setMaxWait(-1);
        druidDataSource.setValidationQuery("select 1");
        druidDataSource.setTestWhileIdle(true);
        // 借出连接时，是否测试，设置为true,避免由于长链接失效导致的异常
        druidDataSource.setTestOnBorrow(true);
        // 归还连接时，是否测试
        druidDataSource.setTestOnReturn(false);
        // 设置空闲连接回收器每隔 30s 运行一次
        druidDataSource.setTimeBetweenEvictionRunsMillis(30 * 1000L);
        // 设置池中连接空闲 30min 被回收，默认值即为 30 min
        druidDataSource.setMinEvictableIdleTimeMillis(30 * 60 * 1000L);
    }
    public static Connection getConnection() throws SQLException {
        DruidPooledConnection conn = druidDataSource.getConnection();
        return conn;
    }
}
